package com.renwei.yunlong.view;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.renwei.yunlong.R;
import com.renwei.yunlong.cn.sharesdk.onekeyshare.OnekeyShare;
import com.renwei.yunlong.utils.LogUtil;
import com.renwei.yunlong.http.Api;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import github.opensource.dialog.BeToastUtil;

/**
 * Project :  yunaandroid.
 * Package name: com.renwei.yunlong.icon_work_watch
 * Created by :  Benjamin.
 * Created time: 2017/12/18 18:42
 * Changed by :  Benjamin.
 * Changed time: 2017/12/18 18:42
 * Class description:
 */

public class SharePopupWindow extends BasePopwindow implements View.OnClickListener, PlatformActionListener {
    private Context context;
    private final String title = "云呐IT交互服务台";
    private final String titleUtl = Api.$().ShareUrl;
    private final String text = "值得信赖的IT服务管理专家";
    private final String shareIcon = "http://ocpo7eh90.bkt.clouddn.com/%E7%BB%8432@2x.png";
    private final String url = Api.$().ShareUrl;
    private final String comment = "值得信赖的IT服务管理专家";
    private final String site = "云呐IT交互服务台";
    private final String siteUrl = Api.$().ShareUrl;

    public SharePopupWindow(Context context) {
        super(context, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, R.style.ActionSheetDialogAnimation);
        this.context = context;
        OnekeyShare oks = new OnekeyShare();
        initView();
    }

    void initView() {
        View shareView;
        shareView = LayoutInflater.from(context).inflate(R.layout.layout_share_pop, null, false);
        shareView.findViewById(R.id.share_qq_friend).setOnClickListener(this);
        shareView.findViewById(R.id.share_qq_space).setOnClickListener(this);
        shareView.findViewById(R.id.share_wechat_friend).setOnClickListener(this);
        shareView.findViewById(R.id.share_wechat_space).setOnClickListener(this);
        shareView.findViewById(R.id.share_cancle).setOnClickListener(this);
        if (shareView != null) {
            initUI(shareView);
        } else {
            LogUtil.e("empty layout");
        }
    }

    public void show(View parent) {
        showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.share_qq_friend:
                // TODO: 2017/12/18 qq好友分享
                QQ.ShareParams qq_sp = new QQ.ShareParams();
                qq_sp.setShareType(Platform.SHARE_WEBPAGE);
                qq_sp.setTitle(title);
                qq_sp.setText(text);
                qq_sp.setUrl(url);
                qq_sp.setTitleUrl(titleUtl);
                qq_sp.setSite(site);
                qq_sp.setSiteUrl(siteUrl);
                qq_sp.setImageUrl(shareIcon);
                qq_sp.setComment(comment);
                Platform qq = ShareSDK.getPlatform(QQ.NAME);
                qq.setPlatformActionListener(this); // 设置分享事件回调
                // 执行分享
                qq.share(qq_sp);
                break;
            case R.id.share_qq_space:
                // TODO: 2017/12/18 qq控件分享
                QZone.ShareParams qzone_sp = new QZone.ShareParams();
                qzone_sp.setShareType(Platform.SHARE_WEBPAGE);
                qzone_sp.setTitle(title);
                qzone_sp.setText(text);
                qzone_sp.setUrl(url);
                qzone_sp.setTitleUrl(titleUtl);
                qzone_sp.setSite(site);
                qzone_sp.setSiteUrl(siteUrl);
                qzone_sp.setImageUrl(shareIcon);
                qzone_sp.setComment(comment);
                qzone_sp.setImageUrl(shareIcon);
                Platform qzone = ShareSDK.getPlatform(QZone.NAME);
                qzone.setPlatformActionListener(this); // 设置分享事件回调
                // 执行图文分享
                qzone.share(qzone_sp);
                break;
            case R.id.share_wechat_friend:
                // TODO: 2017/12/18 微信好友分享
                Wechat.ShareParams wechat_sp = new Wechat.ShareParams();
                wechat_sp.setShareType(Platform.SHARE_WEBPAGE);
                wechat_sp.setTitle(title);
                wechat_sp.setText(text);
                wechat_sp.setUrl(url);
                wechat_sp.setTitleUrl(titleUtl);
                wechat_sp.setSite(site);
                wechat_sp.setSiteUrl(siteUrl);
                wechat_sp.setImageUrl(shareIcon);
                wechat_sp.setComment(comment);
                Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
                wechat.setPlatformActionListener(this); // 设置分享事件回调
                // 执行分享
                wechat.share(wechat_sp);
                break;
            case R.id.share_wechat_space:
                // TODO: 2017/12/18 微信朋友圈分享
                WechatMoments.ShareParams weichatmomnent_sp = new WechatMoments.ShareParams();
                weichatmomnent_sp.setShareType(Platform.SHARE_WEBPAGE);
                weichatmomnent_sp.setTitle(title);
                weichatmomnent_sp.setText(text);
                weichatmomnent_sp.setUrl(url);
                weichatmomnent_sp.setTitleUrl(titleUtl);
                weichatmomnent_sp.setSite(site);
                weichatmomnent_sp.setSiteUrl(siteUrl);
                weichatmomnent_sp.setImageUrl(shareIcon);
                weichatmomnent_sp.setComment(comment);
                Platform wechatMoments = ShareSDK.getPlatform(WechatMoments.NAME);
                wechatMoments.setPlatformActionListener(this); // 设置分享事件回调
                // 执行分享
                wechatMoments.share(weichatmomnent_sp);
                break;
            case R.id.share_cancle:
                this.dismiss();
                break;
            default:
                break;
        }
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        BeToastUtil.get().showCenterSuccessMsg(context, "分享成功");
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        BeToastUtil.get().showCenterInfoMsg(context, throwable.getMessage());
    }

    @Override
    public void onCancel(Platform platform, int i) {
        BeToastUtil.get().showCenterInfoMsg(context, "取消分享");
    }
}
